package com.example.nettyclient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.nettyclient.base.BaseActivity;
import com.example.nettyclient.bean.MessageBean;
import com.example.nettyclient.databinding.ActivityMainBinding;
import com.example.nettyclient.netty.NettyTcpClient;
import com.example.nettyclient.netty.config.Config;
import com.example.nettyclient.netty.listener.NettyClientListener;
import com.example.nettyclient.netty.status.ConnectState;

import java.util.List;

public class MainActivity extends BaseActivity implements NettyClientListener<String> {
    private static final String TAG = "MainActivity";
    ActivityMainBinding activityMainBinding;
    private NettyTcpClient mNettyTcpClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        //Netty客户端的搭建
        mNettyTcpClient = new NettyTcpClient.Builder()
                .setHost(Config.inetHost)  //设置服务端地址
                .setTcpPort(Config.inetPort)  //设置服务端端口号
                .setMaxReconnectTimes(5)  //设置最大重连次数
                .setReconnectIntervalTime(5)    //设置重连间隔时间, 单位：秒
                .setSendheartBeat(true) //设置是否发送心跳
                .setHeartBeatInterval(5)    //设置心跳间隔时间, 单位：秒
                .setHeartBeatData(Config.heartBeatData) //设置心跳数据，可以是String类型，也可以是byte[]，以后设置的为准
                .setIndex(0)    //设置客户端标识.(因为可能存在多个tcp连接)
                .build();

        mNettyTcpClient.setListener(MainActivity.this); //设置TCP监听
        mSharedViewModel.mNettyTcpClient.setValue(mNettyTcpClient);
        mSharedViewModel.connectState.setValue(true);
        mSharedViewModel.loginState.setValue(false);

        //connect();

        mSharedViewModel.connectState.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(!aBoolean){
                    showLongToast("连接失败");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnect();
    }

    private void connect() {
        Log.d(TAG, "connect");
        if (!mNettyTcpClient.getConnectStatus()) {
            mNettyTcpClient.connect();//连接服务器
        } else {
            mNettyTcpClient.disconnect();
        }
    }

    public void disconnect() {
        mNettyTcpClient.disconnect();
    }


    @Override
    public void onMessageResponseClient(String msg, int index) {
        Log.e(TAG, "onMessageResponse:" + msg);
        if(msg.equals(Config.loginSuccess)){
            mSharedViewModel.loginState.setValue(true);
            return;
        }
        if(mSharedViewModel.loginState.getValue()){
            List<MessageBean> tempList = mSharedViewModel.allMessages.getValue();
            tempList.add(new MessageBean(msg, 1));
            mSharedViewModel.allMessages.setValue(tempList);
        }
        return;
    }

    @Override
    public void onClientStatusConnectChanged(int statusCode, int index) {
        if (statusCode == ConnectState.STATUS_CONNECT_SUCCESS) {
            mSharedViewModel.connectState.setValue(true);
        }else {
            mSharedViewModel.connectState.setValue(false);
        }
        return;
    }

    @Override
    public void onClientConnectState(boolean success) {
        if(success){
            mSharedViewModel.connectState.setValue(true);
        }else {
            mSharedViewModel.connectState.setValue(false);
        }
    }

}
